package com.apex.lottery.controller.converter;

import com.apex.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.apex.lottery.common.exception.ControllerException;
import com.apex.lottery.controller.result.FindPrizeListResult;
import com.apex.lottery.service.dto.PageListDTO;
import com.apex.lottery.service.dto.PrizeDTO;

import java.util.stream.Collectors;

/**
 * @ClassName AwardConverter
 * @Description controller层转换方法
 * @Author ZJX
 * @Date 2025/4/19 17:21
 * @Version 1.0
 **/
public class AwardConverter {
    public static FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if (pageListDTO == null){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }

        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream()
                        .map(prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );

        return result;
    }
}
